package org.qiaice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.qiaice.entity.table.User;
import org.qiaice.expcetion.CustomException;
import org.qiaice.mapper.UserMapper;
import org.qiaice.service.UserService;
import org.qiaice.tool.TokenUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public void register(String uname, String passwd, String passwd2) {
        if (!passwd.equals(passwd2)) throw new CustomException("前后两段密码不一致");
        User record = lambdaQuery().eq(User::getUname, uname).one();
        if (record != null) throw new CustomException("用户名" + uname + "已被占用");
        passwd = DigestUtils.md5DigestAsHex(passwd.getBytes(StandardCharsets.UTF_8));
        if (!save(new User(null, uname, passwd))) throw new CustomException("用户注册失败");
    }

    @Override
    public Map<String, Object> login(String uname, String passwd) {
        User record = lambdaQuery().eq(User::getUname, uname).one();
        if (record == null) throw new CustomException("用户" + uname + "未注册");
        passwd = DigestUtils.md5DigestAsHex(passwd.getBytes(StandardCharsets.UTF_8));
        if (!record.getPasswd().equals(passwd))
            throw new CustomException("用户名或密码错误");
        return TokenUtil.createToken(record);
    }

    @Override
    public void reset(String uname, String passwd, String passwd2) {
        User record = lambdaQuery().eq(User::getUname, uname).one();
        if (record == null) throw new CustomException("用户" + uname + "未注册");
        String tmpPasswd = DigestUtils.md5DigestAsHex(passwd.getBytes(StandardCharsets.UTF_8));
        if (!record.getPasswd().equals(tmpPasswd))
            throw new CustomException("用户名或密码错误");
        if (passwd.equals(passwd2)) throw new CustomException("新旧密码不能一致");
        passwd2 = DigestUtils.md5DigestAsHex(passwd2.getBytes(StandardCharsets.UTF_8));
        if (!lambdaUpdate().eq(User::getUname, uname).set(User::getPasswd, passwd2).update())
            throw new CustomException("重置用户密码失败");
    }
}
